package net.shop.action.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import net.shop.biz.db.ProductService;
import net.shop.biz.db.ProductVirtualCategoryService;
import net.shop.biz.service.FriendlyUrlService;
import net.shop.common.ShopPager;
import net.shop.dal.entity.Product;
import net.shop.dal.entity.ProductVirtualCategory;

import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.Result;
import ths.web.action.exception.ActionNotFoundException;
import ths.web.action.result.ResultView;
import ths.web.annotation.HttpAction;
import ths.web.util.ParamUtils;


@HttpAction
public class ProductAction extends BaseFrontAction {
	
	@Resource
	private ProductVirtualCategoryService productVirtualCategoryService;
	@Resource
	private ProductService productService;
	@Resource
	private FriendlyUrlService friendlyUrlService;
	
	public Result execute() throws ActionException {
		ActionContext context = ActionContext.getContext();	
		HttpServletRequest request = context.getRequest();
		
		String parent = ParamUtils.getTrimParameter(request, "parent");
		String friendlyUrl = ParamUtils.getTrimParameter(request, "friendly_url");
	
		if (StringUtils.isEmpty(friendlyUrl) || StringUtils.isEmpty(parent)) {
			throw new ActionNotFoundException("Category not found.");
		}
		
		long haseCode = friendlyUrlService.getUrlHaseCode(friendlyUrl);
		short languageId = context.getLocaleInfo().getLanguage().getLanguageId();
		
		ProductVirtualCategory category = productVirtualCategoryService.findByHaseCode(languageId, haseCode);
		
		ShopPager<Product> pager = new ShopPager<Product>();
		List<Product> products = productService.getProductPager(category, pager).getResult();
	
		ResultView view = getViewResult(context);
		view.assign("username", "TianHaishen");
		view.assign("products", products);
		
		return view.toView("product_picture_pic");
	}
}
